package com.hgyc.mom.eam.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hgyc.mom.tool.strategy.AutoCodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.eam.mapper.EamCheckReportMapper;
import com.hgyc.mom.eam.entity.EamCheckReport;
import com.hgyc.mom.eam.entity.EamCheckReportLine;
import com.hgyc.mom.eam.service.EamCheckReportLineService;
import com.hgyc.mom.eam.vo.EamCheckReportVO;
import com.hgyc.mom.eam.service.EamCheckReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 设备点检Service业务层实现
 *
 * @author fwj
 * @since 2025-08-14
 */
@Slf4j
@Service
public class EamCheckReportServiceImpl extends ServiceImpl<EamCheckReportMapper, EamCheckReport> implements EamCheckReportService {

    @Autowired
    private EamCheckReportLineService eamCheckReportLineService;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCheckReport(EamCheckReportVO eamCheckReportVO) {
        String equipmentCheckCode = autoCodeUtil.genSerialCode("equipment_check_code", null);
        eamCheckReportVO.setCode(equipmentCheckCode);
        save(eamCheckReportVO);

        //新增设备点检记录
        List<EamCheckReportLine> reportLines = eamCheckReportVO.getAddReportLines();
        if (!CollectionUtils.isEmpty(reportLines)) {
            reportLines.forEach(item -> {
                item.setId(null);
                item.setReportId(eamCheckReportVO.getId());
            });
            eamCheckReportLineService.saveBatch(reportLines);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCheckReport(EamCheckReportVO eamCheckReportVO) {
        // TODO 其他逻辑
        updateById(eamCheckReportVO);

        //删除设备点检记录
        if (!CollectionUtils.isEmpty(eamCheckReportVO.getDeleteReportLineIds())) {
            eamCheckReportLineService.removeBatchByIds(eamCheckReportVO.getDeleteReportLineIds());
        }

        //新增设备点检记录
        List<EamCheckReportLine> reportLines = eamCheckReportVO.getAddReportLines();
        if (!CollectionUtils.isEmpty(reportLines)) {
            reportLines.forEach(item -> {
                item.setId(null);
                item.setReportId(eamCheckReportVO.getId());
            });
            eamCheckReportLineService.saveBatch(reportLines);
        }

        //更新设备点检记录
        if (!CollectionUtils.isEmpty(eamCheckReportVO.getUpdateReportLines())) {
            List<EamCheckReportLine> updateReportLines = eamCheckReportVO.getUpdateReportLines();
            eamCheckReportLineService.updateBatchById(updateReportLines);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCheckReport(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除设备点检记录
        eamCheckReportLineService.deleteBatchByParentIds(ids);
    }

    @Override
    public void updateStatus(String status, List<Long> ids) {
        LambdaUpdateWrapper<EamCheckReport> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(EamCheckReport::getId, ids);
        updateWrapper.set(EamCheckReport::getStatus, status);
        update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<EamCheckReport> eamCheckReportList, boolean updateSupport) {
        if (eamCheckReportList == null || eamCheckReportList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + eamCheckReportList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + eamCheckReportList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("设备点检导入失败", e);
            throw new BizException("设备点检导入失败：" + e.getMessage());
        }
    }
}
